package com.wd.live.module_wide.viewmodel

import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.GsonUtils.fromJson
import com.wd.live.base.socket.WebSocketUtils
import com.wd.live.module_base.ui.base.BaseResult
import com.wd.live.module_base.net.bean.wide.FindLiveListBean
import com.wd.live.module_base.net.bean.wide.FindLiveTypeListBean
import com.wd.live.module_base.net.bean.wide.WatchLiveBean
import com.wd.live.module_base.net.bean.wide.WebsocketData
import com.wd.live.module_base.net.util.HttpUtils
import com.wd.live.module_base.net.util.HttpUtils2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import retrofit2.http.Query

/**
 * 作者: 王晶晶
 * 时间:2024/5/22
 * 描述:
 */
class WideViewModel :ViewModel(){
    var agoraToken = ""
    var channelName = ""
    var liveUid = 0
    //直播列表
    val dataFindLiveList=MutableLiveData<BaseResult<List<FindLiveListBean>>>()
        fun getFindLiveList(liveType:Int, page:Int, size:Int) {
        viewModelScope.launch(Dispatchers.IO) {
            var  result = HttpUtils2.apiService.getFindLiveList(liveType, page, size)
            if (result.status =="0000"){
                dataFindLiveList.postValue(result)
                //主线程
                withContext(Dispatchers.Main){}
            }
        }
    }
    //直播分类
    val dataFindLiveTypeList=MutableLiveData<BaseResult<List<FindLiveTypeListBean>>>()

    fun  getFindLiveTypeList(){
        viewModelScope.launch ( Dispatchers.IO ){
            var result= HttpUtils2.apiService.getFindLiveTypeList()
            if (result.status=="0000"){
                dataFindLiveTypeList.postValue(result)
            }
        }
    }
    //看直播
    val dataWatchLive=MutableLiveData<BaseResult<WatchLiveBean>>()

    fun getWatchLive( liveId:Int){
        viewModelScope.launch ( Dispatchers.IO ){
            var result= HttpUtils2.apiService.getWatchLive(liveId)
            if (result.status=="0000"){
                dataWatchLive.postValue(result)
            }
        }
    }
    private var webSocketUtils: WebSocketUtils? = null

    val mLiveBase = MutableLiveData<WebsocketData.LiveBase?>()
    val mPersonCount = MutableLiveData<WebsocketData.PersonCount?>()
    val mBridge = MutableLiveData<WebsocketData.Bridge?>()
    val mConcat = MutableLiveData<WebsocketData.Concat?>()
    var compositeWsUrl = ""
    /**
     * 连接webSocket
     */
    fun connectSocket(context: Context) {
        //开启ws连接
        webSocketUtils = WebSocketUtils().startConnect(compositeWsUrl, context)
        //消息监听
        webSocketUtils!!.setOnMessageListener(object : WebSocketUtils.OnMessageListener {
            override fun message(msg: String?) {
                Log.e("======Socked okhttp", msg.toString())
                msg ?: return
                when (JSONObject(msg).getInt("msgType")) {
                    //基础信息
                    101 -> {
                        mLiveBase.value = fromJson(msg, WebsocketData.LiveBase::class.java)
                    }
                    //直播间实时人数更新消息
                    102 -> {
                        mPersonCount.value = fromJson(msg, WebsocketData.PersonCount::class.java)
                    }
                    //鹊桥类消息
                    103 -> {
                        mBridge.value = fromJson(msg, WebsocketData.Bridge::class.java)
                    }
                    //连麦类消息
                    104 -> {
                        mConcat.value = fromJson(msg, WebsocketData.Concat::class.java)
                    }
                }
            }
        })
    }



}